

<template>
  <div class="app-container">
      <div class="head-container">
        <div class="tip">
          <p style="font-weight: 900; font-size: 22px;">文案助手</p>
          <p>温馨提示：参数主题关键词是要生成内容中必须携带的，参照语也可以不填写让他自由发挥！</p>
        </div>
      </div>

    <div class="button-new-tags-el">
      <el-tag
        :key="tag"
        v-for="tag in dynamicTags"
        closable
        :disable-transitions="false"
        @close="handleClose(tag)">
        {{tag}}
      </el-tag>
      <el-input
        class="input-new-tag"
        v-if="inputVisible"
        v-model="inputValue"
        ref="saveTagInput"
        size="small"
        @keyup.enter.native="handleInputConfirm"
        @blur="handleInputConfirm"
      >
      </el-input>
      <el-button v-else class="button-new-tag" size="small" @click="showInput">+ 添加主题关键词</el-button>
    </div>

<!--    <div class="button-new-tags-el">-->
<!--      <div style="margin-top: 20px">-->
<!--        <el-radio v-model="radio2" label="1" border size="medium">参考小红书</el-radio>-->
<!--        <el-radio v-model="radio2" label="2" border size="medium">参考抖音</el-radio>-->
<!--        <el-radio v-model="radio3" label="3" border size="medium">参考快手</el-radio>-->
<!--      </div>-->
<!--    </div>-->
    <el-col :span="1.5">
      功能
      <el-select v-model="funPrompts" ref="optionPromtsRef" placeholder="请选择">
        <el-option
          v-for="item in promptsoptions"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
    </el-col>

    <el-col :span="1.5" style="margin-left: 50px;">
      生成数量
      <el-input-number size="mini" v-model="oneToNum"></el-input-number>
    </el-col>

    <div class="button-new-tags-el" style="height: 100px;padding-top: 52px;">
    <el-col :span="24">
      <el-input
        type="textarea"
        :rows="2"
        placeholder="请输入参照语"
        v-model="referTxt">
      </el-input>
    </el-col>
    </div>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleGen"
        >生成内容</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleReset"
        >重置关键词</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['article:spiderArticle:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
      <el-button
        type="warning"
        plain
        icon="el-icon-download"
        size="mini"
        @click="handleExport"
        v-hasPermi="['article:hottitle:export']"
      >导出列表数据</el-button>
    </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" border :data="hottitleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="生成标题" align="left" width="385" prop="title" />
      <el-table-column label="包含参数" align="center" prop="keywordPs" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
<!--      <el-table-column label="热度" align="center" prop="hotIndex" />-->
<!--      <el-table-column label="来源链接" align="center" prop="spiderUrl" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document-copy"
            @click="handleCopy(scope.row)"
          >复制</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['article:hottitle:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改采集文章标题，爆款标题对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入文章标题" />
        </el-form-item>
        <el-form-item label="文章唯一编码" prop="urlMd5">
          <el-input v-model="form.urlMd5" placeholder="请输入文章唯一编码" />
        </el-form-item>
        <el-form-item label="抓取的链接" prop="spiderUrl">
          <el-input v-model="form.spiderUrl" placeholder="请输入抓取的链接" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHottitle, getHottitle, delHottitle, addHottitle, updateHottitle,genTitle,PromptsSelect } from "@/api/article/hottitle";
import Clipboard from 'clipboard'
export default {
  name: "Hottitle",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      funPrompts:{

      },
      // 非单个禁用
      single: true,
      referTxt:'',
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      oneToNum:1,
      // 采集文章标题，爆款标题表格数据
      hottitleList: [],
      promptsoptions:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // radio1: '1',
      // radio2: '1',
      // radio3: '1',
      // radio4: '1',
      // 备注时间范围
      daterangeCreateTime: [],
      dynamicTags: [],
      inputVisible: false,
      inputValue: '',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        status: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "文章标题不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    PromptsSelect().then(response => {
      console.log(response)
      this.promptsoptions = response.rows;
    });
  },
  methods: {
    handleClose(tag) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
    },
    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.dynamicTags.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = '';
    },
    /** 查询采集文章标题，爆款标题列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listHottitle(this.queryParams).then(response => {
        this.hottitleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        urlMd5: null,
        status: "0",
        spiderUrl: null,
        createTime: null,
        updateTime: null,
        updateBy: null,
        createBy: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加采集文章标题，爆款标题";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getHottitle(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改采集文章标题，爆款标题";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateHottitle(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHottitle(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleCopy(row){
      // const copyContent = row.title;
      const content = row.title;
      navigator.clipboard.writeText(content)
        .then(() => {
          this.$message({message: '复制成功',type: 'success' });
        })
        .catch((error) => {
          this.$message.error('复制失败');
        });


    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除采集文章标题，爆款标题编号为"' + ids + '"的数据项？').then(function() {
        return delHottitle(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    //生成关键词
    handleGen(){
      // genTitle
      let keyWords= this.dynamicTags.join(",");
      var psId =  this.$refs.optionPromtsRef.selected.value;
      var psName =  this.$refs.optionPromtsRef.selected.label;
      let genPs = {
        oneToNum:this.oneToNum,
        keyWords:keyWords,
        psId:psId,
        refer:this.referTxt
        // useModel:this.useModel
      };
      console.log(genPs);
      // if(this.isEmpty(keyWords)){
      //   this.$message.error(`内容参数不能为空!`);
      //   return;
      // }
      if(this.isEmpty(this.oneToNum)){
        this.$message.error(`生成数量不能为空!`);
        return;
      }
      if(this.isEmpty(psId)){
        this.$message.error(`请选择功能!`);
        return;
      }
      this.$modal.confirm('确定按照当前参数生成商品标题？').then(function() {
        return genTitle(genPs);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("任务提交成功！");
      }).catch(() => {});


    },
    isEmpty(str){
      if (!str || str == "undefined" || str == "null") {
        return true;
      }else{
        return false;
      }
    },
    //重置参数
    handleReset(){
      console.log(this.dynamicTags);

      this.$confirm('是否重置主题关键词？?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.dynamicTags = [];
        this.$message({
          type: 'success',
          message: '重置成功!'
        });
      }).catch(() => {
      });



    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('article/hottitle/export', {
        ...this.queryParams
      }, `hottitle_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
  .head-container .tip {
    padding: 8px 16px;
    background-color: #4a67de1f;
    border-radius: 4px;
    border-left: 5px solid #2f54eb;
    margin: 20px 0;
  }
   .el-tag + .el-tag {
     margin-left: 10px;
   }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
  .button-new-tags-el{
    margin-bottom: 20px;
  }
</style>
